﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LCA.Community.DAL.Model;
using LCA.Community.DAL.Interfaces;

namespace LCA.Community.DAL
{
    /// <summary>
    /// The repository for votes data manipulation.
    /// </summary>
    public class PostVotesRepository : BaseRepository<PostVote>, IPostVotesRepository
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PostVotesRepository"/> class.
        /// </summary>
        /// <param name="dataServiceContext">The data service context.</param>
        public PostVotesRepository(IDataServiceContext<PostVote> dataServiceContext)
            :base(dataServiceContext)
        {

        }

        /// <summary>
        /// Gets the post votes count.
        /// </summary>
        /// <param name="postID">The post ID.</param>
        /// <returns>The count of votes of a post.</returns>
        public int GetPostVotesCount(string postID)
        {
            return (from vote in Entities
                    where vote.Post == postID
                    select vote).Count();
        }

        /// <summary>
        /// Determines if the user has voted the post.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        /// <param name="postID">The post ID.</param>
        /// <returns>True is the user has votes the post, otherwise false.</returns>
        public bool UserHasVotedPost(string userID, string postID)
        {
            return (from vote in Entities
                    where vote.Post == postID && vote.User == userID
                    select vote).Any();
        }

        /// <summary>
        /// Gets the post votes by post.
        /// </summary>
        /// <param name="postID">The post ID.</param>
        /// <returns></returns>
        public List<PostVote> GetPostVotesByPost(string postID)
        {
            return (from vote in Entities
                    where vote.Post == postID
                    select vote).ToList();
        }

        /// <summary>
        /// Deletes all post votes from post.
        /// </summary>
        /// <param name="postID">The post ID.</param>
        public void DeleteAllPostVotesFromPost(string postID)
        {
            var votes = GetPostVotesByPost(postID);

            votes.ForEach(v => Delete(v));
        }

        /// <summary>
        /// Gets the post votes by user.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        /// <returns></returns>
        public List<PostVote> GetPostVotesByUser(string userID)
        {
            return (from vote in Entities
                    where vote.User == userID
                    select vote).ToList();
        }

        /// <summary>
        /// Deletes all post votes from user.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        public void DeleteAllPostVotesFromUser(string userID)
        {
            var votes = GetPostVotesByUser(userID);

            votes.ForEach(v => Delete(v));
        }
    }
}
